<?php
App::uses('AppController', 'Controller');

class LoginController extends AppController {

	public function beforefilter() {
        $this->Auth->logoutRedirect = array(
            "controller" => "pages",
            "action" => "display"
        );
		if (!$this->isLogged()) {
			$this->Auth->allow(array('index', 'login'));
		} else {
			if ($this->request->action != 'logout') {
				return $this->redirect(array('controller' => 'pages', 'action' => 'display', 'home'));	
			}
		}
        $this->layout = "login";
	}

	public function index() {
		
	}

	public function login() {
		if ($this->request->is('post')) {
			 if ($this->Auth->login() ) {

                 $user = $this->Auth->user();
                 if (!$user['activated']) {
                    $this->Session->setFlash(__('Użytkownik nie został jeszcze aktywowany.'), 'warning');
                    $this->redirect($this->Auth->logout());
                 }

				return $this->redirect(array('controller' => 'pages', 'action' => 'display', 'home'));
			 } else {
			 	$this->Session->setFlash(__('Podano niepoprawny login lub hasło.'), 'error');
			 	return $this->redirect(array('action' => 'index'));
			 }
		} else {
			return $this->redirect(array('action' => 'index'));
		}
	}

	public function logout() {
		$this->Session->setFlash(__('Poprawnie wylogowano.'), 'info');
    	return $this->redirect($this->Auth->logout());
	}
}